package com.car.web.admin;

import com.car.domain.Admin;
import com.car.server.impl.AdminLogServerImpl;
import org.apache.commons.beanutils.BeanUtilsBean;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;

/**
 * 判断账号密码是否正确
 * 正确登陆
 * 否则返回登陆界面
 */
@WebServlet("/log/AdminLogWeb")
public class AdminLogWeb extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取req并且封装到admin中
        Admin admin = new Admin();
        try {
            new BeanUtilsBean().populate(admin,req.getParameterMap());
            //调用方法进行处理
            admin = new AdminLogServerImpl().logCheck(admin);
            if (admin!=null){
                //这里查找到了
                //先将id保存到session中
                req.getSession().setAttribute("admin",admin);
                //之后跳转到main.jsp
                resp.sendRedirect(req.getContextPath()+"../admin/main.jsp");
            }else{
                //未能查找到
                req.setAttribute("log","登陆失败，账号或密码错误");
                req.getRequestDispatcher("log.jsp").forward(req,resp);
            }
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }
}
